package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.*;
import cn.gson.crm.model.mapper.DeptMapper;
import cn.gson.crm.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class DeptService {
    @Autowired
    DeptMapper deptMapper;
    @Autowired
    RoleDao roleDao;
    @Autowired
    DeptFunctionsDao deptFunctionsDao;
    @Autowired
    DeptDao deptDao;
    @Autowired
    FunctionsDao functionsDao;
    @Autowired
    FunctionDao functionDao;
    public List<Dept> selectdet(Integer did){
        return deptMapper.selectdet(did);
    };

    //根据部门id查询相对应职务
    public List<Role> selectrow(Integer did){
        return deptMapper.selectrow(did);
    };
    //查询全部角色
    public List<Role> selectrowquan(){
        return deptMapper.selectrowquan();
    };
    //给部门新增角色
    public void save(String positionList) {
        JSONObject object = (JSONObject) JSONObject.parse(positionList);
        Integer deptId = (Integer) object.get("deptId");//取到部门id
        Dept dept = new Dept();//实列化部门对象
        dept.setDeptId(deptId);
        Role position = null;
        JSONObject jsonObject = null;
        List list = object.getObject("positionList", List.class);
        JSONArray array = (JSONArray) JSONArray.parseArray(String.valueOf(list));
        for (int i = 0; i < array.size(); i++) {
            jsonObject = (JSONObject) array.get(i);
            String postName = jsonObject.getString("value");
            position=new Role();
            position.setDept(dept);
            position.setjName(postName);
            position.setjZil((long) 2);//默认设置为二级角色
            roleDao.save(position);
        }

    }
    //查询所有的功能组
    public List<Functions> selectfun(){
        return deptMapper.selectfun();
    };
    //根据部门查询功能组
    public List<Integer> selectwherfun(Integer did){
        return deptMapper.selectwherfun(did);
    };
    //给部门授权功能
    public void busave(String position){
        JSONObject object = (JSONObject)JSONObject.parse(position);
        JSONObject jsonObject = null;
        List list = object.getObject("juese",List.class);
        Long ji = object.getLong("ji");
      Dept dept=deptDao.findById(ji).get();
      List<Functions> functionsList=dept.getFunctionsList();
        if(functionsList.size()==0){
            functionsList = new ArrayList<>();
        }
        //给permissionssetList添加对象
        functionsList.clear();
        functionsList.addAll((List)functionsDao.findAllById(list));
        //重新给集合赋值,相当于执行中间表的新增
        dept.setFunctionsList(functionsList);

    }
    //根据部门id查询功能组和功能
    public List<Function> selectfuntis(Integer did){
        return  deptMapper.selectfuntis(did);
    };
    //查询所有功能
    public List<Integer> selectgongsuoy(Integer jid){
        return deptMapper.selectgongsuoy(jid);
    };
    //给功能组加添加功能
    public void savegongge(String position){
        JSONObject object = (JSONObject)JSONObject.parse(position);
        JSONObject jsonObject = null;
        List list = object.getObject("gonge",List.class);
        Long ji = object.getLong("jid");
        Role role=roleDao.findById(ji).get();
        List<Function> functionList=role.getFunctionList();
        if(functionList.size()==0){
            functionList = new ArrayList<>();
        }
        //给permissionssetList添加对象
        functionList.clear();
        functionList.addAll((List)functionDao.findAllById(list));
        //重新给集合赋值,相当于执行中间表的新增
        role.setFunctionList(functionList);
    }
    //查询所有部门
    public List<Dept> selectdeptd(){
       return deptMapper.selectdeptd();
    };
    //新增部门
    public void savedept(Dept dept){
        deptDao.save(dept);
        if(dept.getDeptId()>0){
            Role role=new Role();
            role.setjZil((long) 1);
            role.setjName("经理");
            role.setDept(dept);
            roleDao.save(role);
        }
    }
    //根据部门名称查询部门
    public List<Dept> selectbuname(String dname){
        return  deptMapper.selectbuname(dname);
    };
    //根据部门查询此部门下有没有员工
    public List<User> selectuser(Integer did){
        return deptMapper.selectuser(did);
    };
    //删除部门
    public void deletedept(Long did){
        deptDao.deleteById(did);
    }
    //根据部门id查询上级部门
    public List<Dept> selectdepet4(Integer did){
        return  deptMapper.selectdepet4(did);
    };
    //查询此部门有没有下级部门
    public List<Dept> selectxiadept(Integer jid){
        return deptMapper.selectxiadept(jid);
    }
    //根据角色名查询角色
    public List<Role> selectjueses(String jname,String did){
        return deptMapper.selectjueses(jname,did);
    };
    //查询此角色有没有被员工引用
    public List<UserRole> seelctuserrole(Integer jid){
        return deptMapper.seelctuserrole(jid);
    };
    //删除角色
    public void deletjuese(Long jid){
        roleDao.deleteById(jid);
    }
    //修改角色
    public void updaterow(String jname, Integer jid){
        deptMapper.updaterow(jname,jid);
    };
    //修改一级
    public void updatejuss(Integer jid,Integer did){
        deptMapper.updatejuss(1,jid);
        List<Role> roleList=deptMapper.selectrow(did);
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjId()!=jid){
                deptMapper.updatejuss(2, (int) roleList.get(i).getjId());
            }
        }
    }
}
